
/** Dialog to edit settings.
 *
 * Created on September 20, 2007, 7:17 PM
 *
 * @author  pquiring
 */

import javaforce.*;

public class EditSettings extends javax.swing.JDialog {

  /** Creates new form EditSettings */
  private EditSettings(java.awt.Frame parent, boolean modal, Settings settings) {
    super(parent, modal);
    this.settings = settings;
    initComponents();
    setTitle("Edit Settings");
    setComponentOrientation(((parent == null) ? javax.swing.JOptionPane.getRootFrame() : parent).getComponentOrientation());
    if (parent != null) setLocationRelativeTo(parent);
    load();
    JFAWT.assignHotKey(this, bOk, java.awt.event.KeyEvent.VK_ENTER);
    JFAWT.assignHotKey(this, bCancel, java.awt.event.KeyEvent.VK_ESCAPE);
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    bOk = new javax.swing.JButton();
    bCancel = new javax.swing.JButton();
    sFontSize = new javax.swing.JSpinner();
    jLabel1 = new javax.swing.JLabel();
    cbClean = new javax.swing.JCheckBox();
    jLabel2 = new javax.swing.JLabel();
    sTabSize = new javax.swing.JSpinner();
    cbAutoIndent = new javax.swing.JCheckBox();
    cbLineWrap = new javax.swing.JCheckBox();
    cbPreserve = new javax.swing.JCheckBox();
    eol = new javax.swing.JComboBox<>();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    cbTabToSpaces = new javax.swing.JCheckBox();

    FormListener formListener = new FormListener();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setResizable(false);
    addKeyListener(formListener);

    bOk.setMnemonic('o');
    bOk.setText("OK");
    bOk.addActionListener(formListener);
    bOk.addKeyListener(formListener);

    bCancel.setMnemonic('c');
    bCancel.setText("Cancel");
    bCancel.addActionListener(formListener);
    bCancel.addKeyListener(formListener);

    sFontSize.addChangeListener(formListener);
    sFontSize.addKeyListener(formListener);

    jLabel1.setText("Font Size");

    cbClean.setText("Trim trailing spaces when saving");
    cbClean.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    cbClean.setMargin(new java.awt.Insets(0, 0, 0, 0));
    cbClean.addKeyListener(formListener);

    jLabel2.setText("Tab Size");

    sTabSize.addChangeListener(formListener);
    sTabSize.addKeyListener(formListener);

    cbAutoIndent.setText("Auto indent");
    cbAutoIndent.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    cbAutoIndent.setMargin(new java.awt.Insets(0, 0, 0, 0));

    cbLineWrap.setText("Line wrap");
    cbLineWrap.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    cbLineWrap.setMargin(new java.awt.Insets(0, 0, 0, 0));
    cbLineWrap.addActionListener(formListener);
    cbLineWrap.addKeyListener(formListener);

    cbPreserve.setText("Preserve line endings (CRLF or LF)");
    cbPreserve.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    cbPreserve.setMargin(new java.awt.Insets(0, 0, 0, 0));

    eol.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "CRLF", "LF" }));

    jLabel3.setText("Create new files in");

    jLabel4.setText("EOL format.");

    cbTabToSpaces.setText("Convert tabs to spaces");
    cbTabToSpaces.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    cbTabToSpaces.setMargin(new java.awt.Insets(0, 0, 0, 0));
    cbTabToSpaces.setPreferredSize(new java.awt.Dimension(131, 15));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(bOk)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bCancel))
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jLabel1)
              .addComponent(jLabel2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
              .addComponent(sTabSize)
              .addComponent(sFontSize, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)))
          .addGroup(layout.createSequentialGroup()
            .addComponent(jLabel3)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(eol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jLabel4))
          .addComponent(cbAutoIndent)
          .addComponent(cbPreserve)
          .addComponent(cbLineWrap)
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
            .addComponent(cbTabToSpaces, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(cbClean, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        .addContainerGap(199, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(sFontSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel2)
          .addComponent(sTabSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(cbPreserve)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(eol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel3)
          .addComponent(jLabel4))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(cbClean)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(cbAutoIndent)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(cbLineWrap)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(cbTabToSpaces, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(bOk)
          .addComponent(bCancel))
        .addContainerGap())
    );

    pack();
  }

  // Code for dispatching events from components to event handlers.

  private class FormListener implements java.awt.event.ActionListener, java.awt.event.KeyListener, javax.swing.event.ChangeListener {
    FormListener() {}
    public void actionPerformed(java.awt.event.ActionEvent evt) {
      if (evt.getSource() == bOk) {
        EditSettings.this.bOkActionPerformed(evt);
      }
      else if (evt.getSource() == bCancel) {
        EditSettings.this.bCancelActionPerformed(evt);
      }
      else if (evt.getSource() == cbLineWrap) {
        EditSettings.this.cbLineWrapActionPerformed(evt);
      }
    }

    public void keyPressed(java.awt.event.KeyEvent evt) {
      if (evt.getSource() == EditSettings.this) {
        EditSettings.this.anykey(evt);
      }
      else if (evt.getSource() == bOk) {
        EditSettings.this.anykey(evt);
      }
      else if (evt.getSource() == bCancel) {
        EditSettings.this.anykey(evt);
      }
      else if (evt.getSource() == sFontSize) {
        EditSettings.this.anykey(evt);
      }
      else if (evt.getSource() == cbClean) {
        EditSettings.this.anykey(evt);
      }
      else if (evt.getSource() == sTabSize) {
        EditSettings.this.anykey(evt);
      }
      else if (evt.getSource() == cbLineWrap) {
        EditSettings.this.anykey(evt);
      }
    }

    public void keyReleased(java.awt.event.KeyEvent evt) {
    }

    public void keyTyped(java.awt.event.KeyEvent evt) {
    }

    public void stateChanged(javax.swing.event.ChangeEvent evt) {
      if (evt.getSource() == sFontSize) {
        EditSettings.this.sFontSizeStateChanged(evt);
      }
      else if (evt.getSource() == sTabSize) {
        EditSettings.this.sTabSizeStateChanged(evt);
      }
    }
  }// </editor-fold>//GEN-END:initComponents

  private void anykey(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_anykey
    int keyCode = evt.getKeyCode();
    int mods = evt.getModifiers();
    if (keyCode == java.awt.event.KeyEvent.VK_ESCAPE && mods == 0) {
      bCancelActionPerformed(null);
    }
    if (keyCode == java.awt.event.KeyEvent.VK_ENTER && mods == 0) {
      bOkActionPerformed(null);
    }
  }//GEN-LAST:event_anykey

  private void sTabSizeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sTabSizeStateChanged
    int tmp = ((Integer)sTabSize.getValue()).intValue();
    if (tmp < MIN_TABSIZE) sTabSize.setValue(MIN_TABSIZE);
    if (tmp > MAX_TABSIZE) sTabSize.setValue(MAX_TABSIZE);
  }//GEN-LAST:event_sTabSizeStateChanged

  private void sFontSizeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sFontSizeStateChanged
    int tmp = ((Integer)sFontSize.getValue()).intValue();
    if (tmp < MIN_FONTSIZE) sFontSize.setValue(MIN_FONTSIZE);
    if (tmp > MAX_FONTSIZE) sFontSize.setValue(MAX_FONTSIZE);
  }//GEN-LAST:event_sFontSizeStateChanged

  private void bCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelActionPerformed
    dispose();
  }//GEN-LAST:event_bCancelActionPerformed

  private void bOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOkActionPerformed
    save();
    dispose();
  }//GEN-LAST:event_bOkActionPerformed

  private void cbLineWrapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbLineWrapActionPerformed
    // TODO add your handling code here:
  }//GEN-LAST:event_cbLineWrapActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton bCancel;
  private javax.swing.JButton bOk;
  private javax.swing.JCheckBox cbAutoIndent;
  private javax.swing.JCheckBox cbClean;
  private javax.swing.JCheckBox cbLineWrap;
  private javax.swing.JCheckBox cbPreserve;
  private javax.swing.JCheckBox cbTabToSpaces;
  private javax.swing.JComboBox<String> eol;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JSpinner sFontSize;
  private javax.swing.JSpinner sTabSize;
  // End of variables declaration//GEN-END:variables

  private Settings settings;
  private static final int MIN_FONTSIZE = 8;
  private static final int MAX_FONTSIZE = 24;
  private static final int MIN_TABSIZE = 2;
  private static final int MAX_TABSIZE = 8;

  public static void editSettings(java.awt.Frame parent, Settings settings) {
    EditSettings dialog = new EditSettings(parent, true, settings);
    dialog.setVisible(true);
  }

  private void load() {
    sFontSize.setValue(settings.fontSize);
    cbPreserve.setSelected(settings.bPreserve);
    eol.setSelectedIndex(settings.bUnix ? 1 : 0);
    cbClean.setSelected(settings.bClean);
    sTabSize.setValue(settings.tabSize);
    cbAutoIndent.setSelected(settings.bAutoIndent);
    cbLineWrap.setSelected(settings.bLineWrap);
    cbTabToSpaces.setSelected(settings.bTabToSpaces);
  }

  private void save() {
    settings.fontSize = ((Integer)sFontSize.getValue()).intValue();
    settings.bPreserve = cbPreserve.isSelected();
    settings.bUnix = eol.getSelectedIndex() == 1;
    settings.bClean = cbClean.isSelected();
    settings.tabSize = ((Integer)sTabSize.getValue()).intValue();
    settings.bAutoIndent = cbAutoIndent.isSelected();
    settings.bLineWrap = cbLineWrap.isSelected();
    settings.bTabToSpaces = cbTabToSpaces.isSelected();
  }

}
